<?php
#by reemo 2012
class ShowDmbuildPage extends AbstractPage
{	
	public static $requireModule = 0;

	function __construct() 
	{
		parent::__construct();
	}
	
	private function CheckLabSettingsInQueue($Element)
	{
		global $PLANET, $CONF;
		if ($PLANET['b_building'] == 0)
			return true;
			
		$CurrentQueue		= unserialize($PLANET['b_building_id']);
		foreach($CurrentQueue as $ListIDArray) {
			if($ListIDArray[0] == $Element)
				return false;
		}

		return true;
	}
	
	private function calcTM($Element)
	{
		global $PLANET, $USER, $resource, $pricelist, $CONF;
		
		$elementLevel = $PLANET[$resource[$Element]];
		
		$metal 		= ceil(($pricelist[$Element]['cost']['901'] * pow($pricelist[$Element]['factor'], $elementLevel)) * 0.00025);
		$crystal	= ceil(($pricelist[$Element]['cost']['902'] * pow($pricelist[$Element]['factor'], $elementLevel)) * 0.0005);
		$deiterium 	= ceil(($pricelist[$Element]['cost']['903'] * pow($pricelist[$Element]['factor'], $elementLevel)) * 0.001);
		$tm	 		= ceil(($pricelist[$Element]['cost']['921'] * pow($pricelist[$Element]['factor'], $elementLevel)) * 1);
		
		$nac 		= $CONF['resource_multiplier'] < 50 ? 1.15 : ($CONF['resource_multiplier'] < 500 ? 1 : 0.33);
		
		$cost 		= ceil($nac * max(1,($metal + $crystal + $deiterium + $tm)));
	
		return($cost);
	}
	
	function up()
	{
		global $PLANET, $USER, $resource, $reslist;
		
		$CurrentMaxFields  	= CalculateMaxPlanetFields($PLANET);
		$Element     		= HTTP::_GP('build', 0);
		$Elements			= $reslist['allow'][$PLANET['planet_type']];
		$costTM =  $this->calcTM($Element);
		if (!$this->CheckLabSettingsInQueue($Element) ||  $PLANET['field_current'] >= $CurrentMaxFields || !in_array($Element,$Elements) || $costTM > $USER['darkmatter'] || !BuildFunctions::isTechnologieAccessible($USER, $PLANET, $Element))
		{
			$this->redirectTo('game.php?page=dmbuild');
			return;
		}
		
		$USER['darkmatter'] -= $costTM;
		$PLANET['field_current'] +=1;
		$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET
				 `field_current` = `field_current` + 1,
				 `".$resource[$Element]."` = `".$resource[$Element]."` + 1
				 WHERE `id` = '".$PLANET["id"]."';");		
		
		#$this->printMessage('Успешно улучшено!',true,  array("game.php?page=dmbuild", 1));
		$this->redirectTo('game.php?page=dmbuild');
	}
	
		
	public function show()
	{
		global $ProdGrid, $LNG, $resource, $reslist, $CONF, $PLANET, $USER, $pricelist;
		
		$BuildInfoList      = array();

		$Elements			= $reslist['allow'][$PLANET['planet_type']];
		
		foreach($Elements as $ID => $Element)
		{
			if (!$this->CheckLabSettingsInQueue($Element) || !BuildFunctions::isTechnologieAccessible($USER, $PLANET, $Element))
				continue;
			
			$costTM =  $this->calcTM($Element);
			$BuildInfoList[$Element]	= array(
				'level'				=> $PLANET[$resource[$Element]],
				'maxLevel'			=> $pricelist[$Element]['max'],
				'costTM'			=> $costTM,
			);
		}

		
		
		$this->tplObj->assign_vars(array(
			'BuildInfoList'			=> $BuildInfoList,
			'planet_field_current' 	=> $PLANET['field_current'],
			'planet_field_max' 		=> CalculateMaxPlanetFields($PLANET),
			'raz'					=> max(0, min(100,round($PLANET['field_current']/CalculateMaxPlanetFields($PLANET)*100))),
		));
			
		$this->display('page.dm.build.tpl');
	}
}
?>